﻿using System;
using System.Windows.Controls;
using EzDesk.DesktopClient.UtilityView.ViewModel;
using EzDesk.MVVMBase;

namespace EzDesk.DesktopClient.ViewModel
{
    using ViewContract;
    using MVVMEnhance;
    using Event;

    public class ShellViewModel : ViewModelWithSingleContainer<IShellView>, IShellViewModel
    {
        public ISelfInfoViewModel SelfInfoViewModel { get; set; }

        public ITopPanelViewModel TopPanelViewModel { get; private set; }

        public IViewModel MainViewModel { get; set; }

        private ISelfInfoViewModel _selfInfoViewModel;

        private IContentTabViewModel _contentTabViewModel;
        private ILoginViewModel _loginViewModel;

        public ShellViewModel(ILoginViewModel loginViewModel,
            ITopPanelViewModel topPanelViewModel,
            ISelfInfoViewModel selfInfoViewModel,
            IContentTabViewModel contentTabViewModel)
            : base()
        {
            _loginViewModel = loginViewModel;
            TopPanelViewModel = topPanelViewModel;
            _selfInfoViewModel = selfInfoViewModel;
            _contentTabViewModel = contentTabViewModel;
            Init();
        }
        public void Shutdown()
        {
            LogoutEvent.Instance.Publish(new LogoutArgs());
            IMClient.CurrentClient.Shutdown();
        }


        private void Init()
        {
            InitEvent();
            InitView();
        }

        private void InitView()
        {
            this.MainViewModel = _loginViewModel;
            RaiseViewModelChanged();
        }
        private void InitEvent()
        {
            LoginEvent.Instance.Subscribe(arg =>
                    {
                        this.MainViewModel = this._contentTabViewModel;
                        this.SelfInfoViewModel = this._selfInfoViewModel;
                        RaiseViewModelChanged();
                    });

            LogoutEvent.Instance.Subscribe(arg =>
                    {
                        this.MainViewModel = this._loginViewModel;
                        this.SelfInfoViewModel = null;
                        RaiseViewModelChanged();
                    });


            View.Activated += (o, e) =>
                MainWindowFocusEvent.Instance.Publish(new MainWindowFocusArgs() { MainWindow = this.View });
            View.Deactivated+= (o, e) =>
                MainWindowFocusEvent.Instance.Publish(new MainWindowFocusArgs() { MainWindow = this.View });
            View.LocationChanged += (o, e) =>
                MainWindowMoveEvent.Instance.Publish(new MainWindowMoveArgs { MainWindow = this.View });
            View.StateChanged +=(o,e)=>
                MainWindowMoveEvent.Instance.Publish(new MainWindowMoveArgs { MainWindow = this.View });
            View.GotFocus += (o, e) =>
                MainWindowMoveEvent.Instance.Publish(new MainWindowMoveArgs { MainWindow = this.View });
            View.GotKeyboardFocus+=(o,e)=>
                MainWindowMoveEvent.Instance.Publish(new MainWindowMoveArgs { MainWindow = this.View });
            View.GotMouseCapture += (o, e) =>
                MainWindowMoveEvent.Instance.Publish(new MainWindowMoveArgs { MainWindow = this.View });
            View.IsKeyboardFocusedChanged += (o, e) =>
                MainWindowMoveEvent.Instance.Publish(new MainWindowMoveArgs { MainWindow = this.View });
            View.IsMouseDirectlyOverChanged += (o, e) =>
                MainWindowMoveEvent.Instance.Publish(new MainWindowMoveArgs { MainWindow = this.View });
        }

        private void RaiseViewModelChanged()
        {
            RaisePropertyChanged("MainViewModel");
            RaisePropertyChanged("SelfInfoViewModel");
        }
    }
}